home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.1v2 SDK / Sample Code / MP Sort Picts 12⁄04⁄99 / Sprocket / Lib / Window.cp < prev   
Encoding:
Text File  |  1999-11-29  |  24.0 KB  |  981 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.                 
  11.     Written by: Dave Falkenburg
  12.  
  13.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  14.  
  15.     Change History (most recent first):
  16.     
  17.          <8>    11/17/94    DRF        Add casts for CFront & PPCC. Also dealt with the change to
  18.                                     ClipAbove in the latest universal headers.
  19.          <7>    11/12/94    DRF        Added AdjustMenusBeforeMenuSelection.
  20.          <6>     11/8/94    DRF        Add some better menu handling methods.
  21.          <5>     9/27/94    DRF         AppLib.h is now Sprocket.h
  22.          <4>      9/9/94    DRF        Reorganized headers and removed redundant #includes.
  23.          <3>      9/4/94    DRF        Added DrawJustTheGrowIcon.
  24.          <2>     8/27/94    DRF        In TWindow::Close, call window’s (de)Activate method before
  25.                                     closing so that menus can be properly updated.
  26.     
  27.     To Do:        Make sure invisible windows can be created & managed
  28.                 Handle modal windows as another class of windows
  29.                 Fix activate bugs when showing and hiding windows
  30.                 Window positioning methods (getters and setters)
  31.                 Display Manager support
  32.                 Changes to support AEObject model
  33.  */
  34.  
  35. #include "Sprocket.h"
  36. #include "Window.h"
  37.  
  38. #include <Script.h>        //    for GetMBarHeight()
  39. #include <MacWindows.h>    //    for FrontNonFloatingWindow()
  40. #include <LowMem.h>        //    for LMGetWindowList()
  41.  
  42.  
  43. const short            kFloatingWindowKind        = 1000;
  44. const short            kNormalWindowKind        = 1001;
  45. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  46.  
  47. const short            kScreenEdgeSlop            = 4;
  48. const short            kSpaceForFinderIcons    = 64;
  49. const short            kMinimumTitleBarHeight    = 21;
  50. const short            kMinimumWindowSize        = 32;
  51.  
  52. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  53.  
  54. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  55. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  56.  
  57.  
  58. struct    CalcWindowAreaDeviceLoopUserData
  59.     {
  60.     GDHandle    fScreenWithLargestPartOfWindow;
  61.     long        fLargestArea;
  62.     Rect        fWindowBounds;
  63.     };
  64.  
  65.  
  66.  
  67.  
  68.  
  69. TWindow::TWindow()
  70.     {
  71.     }
  72.  
  73.  
  74. TWindow::~TWindow()
  75.     {
  76.     }
  77.  
  78.  
  79. void
  80. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  81.     {
  82.     WindowPtr    behindWindow,oldFrontMostWindow;
  83.     
  84.     if (typeOfWindowToCreate == kModalWindow)
  85.         {
  86.         DebugStr((StringPtr) "\pModal windows aren’t supported yet");
  87.         fWindowType = kFloatingWindow;
  88.         return;
  89.         }
  90.     else if (typeOfWindowToCreate == kFloatingWindow)
  91.         {
  92.         behindWindow = (WindowPtr) -1;
  93.         oldFrontMostWindow = FrontWindow();
  94.  
  95.         fWindowType = kFloatingWindow;
  96.         }
  97.     else if (typeOfWindowToCreate == kNormalWindow)
  98.         {
  99.         behindWindow = LastFloatingWindow();
  100.  
  101.         fWindowType = kNormalWindow;
  102.         
  103.         if (behindWindow == kNoFloatingWindows)
  104.             oldFrontMostWindow = nil;
  105.         else
  106.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  107.         }
  108.  
  109.     fWindow = this->MakeNewWindow(behindWindow);
  110.     fIsVisible = ((WindowPeek) fWindow)->visible;
  111.  
  112.     if (fWindow)
  113.         {
  114.         SetWRefCon(fWindow,(long) this);
  115.  
  116.         if (typeOfWindowToCreate == kModalWindow)
  117.             {
  118.             DebugStr((StringPtr) "\pCan’t create Modal windows yet");
  119.             }
  120.         else if (typeOfWindowToCreate == kFloatingWindow)
  121.             {
  122.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  123.             
  124.             //    make sure the other window stays hilited
  125.             if (oldFrontMostWindow)
  126.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  127.             }
  128.         else if (typeOfWindowToCreate == kNormalWindow)
  129.             {
  130.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  131.  
  132.             //    unhighlight the old front window
  133.             if (oldFrontMostWindow)
  134.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  135.  
  136.             //    hilite the new window…
  137.             HiliteAndActivateWindow(fWindow,true);
  138.             }
  139.         }
  140.     }
  141.  
  142.  
  143. void
  144. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  145.     {
  146.     }
  147.  
  148. void
  149. TWindow::Idle(EventRecord * /* anEvent */)
  150.     {
  151.     }
  152.     
  153. void
  154. TWindow::Activate(Boolean /* activating */)
  155.     {
  156.     }
  157.     
  158. void
  159. TWindow::Draw(void)
  160.     {
  161.     }
  162.     
  163. void
  164. TWindow::Click(EventRecord * /* anEvent */)
  165.     {
  166.     }
  167.     
  168. void
  169. TWindow::KeyDown(EventRecord * /* anEvent */)
  170.     {
  171.     }
  172.  
  173.  
  174. void
  175. TWindow::Select(void)
  176.     {
  177.     WindowPtr    currentFrontWindow;
  178.     
  179.     if (fWindowType == kFloatingWindow)
  180.         currentFrontWindow = FrontWindow();
  181.     else if (fWindowType == kNormalWindow)
  182.         currentFrontWindow = FrontNonFloatingWindow();
  183.     else
  184.         {
  185.         }
  186.  
  187.     if (currentFrontWindow != fWindow)
  188.         {
  189.         if (fWindowType == kFloatingWindow)
  190.             BringToFront(fWindow);
  191.         else
  192.             {
  193.             WindowPtr    lastFloater = LastFloatingWindow();
  194.  
  195.             //    If there are no floating windows,
  196.             //    just call SelectWindow like the good ol’ days
  197.  
  198.             if (lastFloater == kNoFloatingWindows)
  199.                 SelectWindow(fWindow);
  200.             else
  201.                 {
  202.                 // Deactivate the window currently in front.
  203.  
  204.                 HiliteAndActivateWindow(currentFrontWindow,false);
  205.     
  206.                 // Bring it behind the last floating window and activate it.
  207.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  208.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  209.                 // this is no longer necessary.
  210.  
  211.                 SendBehind(fWindow,lastFloater);
  212.                 HiliteAndActivateWindow(fWindow,true);
  213.                 }
  214.             }
  215.         }
  216.     }
  217.  
  218.  
  219. void
  220. TWindow::Drag(Point startPoint)
  221.     {
  222.     GrafPtr        savePort;
  223.     KeyMap        theKeyMap;
  224.     Boolean        commandKeyDown = false;
  225.     RgnHandle    draggingRegion;
  226.     long        dragResult;
  227.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  228.     Boolean     gLiveDrag;
  229.     
  230.     
  231.     if (WaitMouseUp())        //    de-bounce?
  232.         {
  233.         // Set up the Window Manager port.
  234.     
  235.         GetPort(&savePort);
  236.         SetPort(gWindowManagerPort);
  237.         SetClip(GetGrayRgn());
  238.  
  239.         // Check to see if the command key is down.
  240.     
  241.         GetKeys(theKeyMap);
  242.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  243.         gLiveDrag = (theKeyMap[1] >> 2) & 0x01 ;
  244.         
  245.         if (commandKeyDown)
  246.             {
  247.             //    We’re not going to change window ordering,
  248.             //    so make sure that we don’t drag in front of
  249.             //    other windows which may be in front of ours.
  250.  
  251. //    11/16/94    <Windows.h> on ETO defines this routine to take
  252. //                a WindowPtr instead of a WindowPeek. When building
  253. //                with new headers (like those included with ETO 16)
  254. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  255.  
  256. #if    USEOLDUNIVERSALHEADERS
  257.             ClipAbove(windowAsWindowPeek);
  258. #else
  259.             ClipAbove((WindowPtr) windowAsWindowPeek);
  260. #endif
  261.             }
  262.         else if (fWindowType != kFloatingWindow)
  263.             {
  264.             //    We’re dragging a normal window, so make sure
  265.             //    that we don’t drag in front of any floating
  266.             //    windows.
  267.  
  268. //    11/16/94    <Windows.h> on ETO defines this routine to take
  269. //                a WindowPtr instead of a WindowPeek. When building
  270. //                with new headers (like those included with ETO 16)
  271. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  272.  
  273. #if    USEOLDUNIVERSALHEADERS
  274.             ClipAbove((WindowPeek) FrontNonFloatingWindow());
  275. #else
  276.             ClipAbove(FrontNonFloatingWindow());
  277. #endif
  278.             }
  279.         
  280.         //    Drag an outline of the window around the desktop.
  281.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  282.  
  283.         //if (gLiveDrag) {
  284.             /*Rect r = {0,0,0,0};
  285.             Point origin = {0,0};    
  286.             Point diff;
  287.             EventRecord event;
  288.             GrafPtr tempPort;
  289.             
  290.             
  291.             GetPort(&tempPort);
  292.             SetPort(fWindow);    
  293.             (void)OSEventAvail(everyEvent,&event);
  294.             
  295.             LocalToGlobal(&origin);
  296.             diff.h = ( origin.h - event.where.h);
  297.             diff.v = ( origin.v -event.where.v);
  298.             //origin.h = windowAsWindowPeek->strucRgn[0]->rgnBBox.left;
  299.             //origin.v = windowAsWindowPeek->strucRgn[0]->rgnBBox.top;
  300.             SetPort(tempPort);
  301.             while (StillDown()) {
  302.                 EventRecord event;
  303.                 //short slop;
  304.                 
  305.                 //(void)OSEventAvail(everyEvent,&event);
  306.                 
  307.                 //ValidRect(&goodRect_Rect);
  308.                 (void)WaitNextEvent(everyEvent,&event,1,nil);
  309.                 
  310.                 if (event.what == updateEvt) {
  311.                     WindowRef window = (WindowRef)event.message;
  312.                     
  313.                     
  314.                     if (window) {
  315.                         TWindow * wobj;
  316.                         
  317.                         SetPort(window);
  318.                         BeginUpdate(window);
  319.                         wobj = GetWindowObject(window);
  320.  
  321.                         wobj->Draw();
  322.                         EndUpdate(window);
  323.                         SetPort(tempPort);
  324.                     }
  325.                     
  326.                     
  327.                 }
  328.                 
  329.                 event.where.h += diff.h ;
  330.                 event.where.v += diff.v;
  331.                 
  332.  
  333.                 
  334.                 
  335.                 if (coplandTask)
  336.                 MoveWindow(fWindow,event.where.h,event.where.v,true);
  337.  
  338.                 Idle(nil);
  339.                 
  340.                 dragResult = 0;
  341.                 
  342.             }
  343.             */
  344.         //} else {
  345.             draggingRegion = NewRgn();
  346.             CopyRgn(windowAsWindowPeek->strucRgn,draggingRegion);
  347.             dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  348.             DisposeRgn(draggingRegion);
  349.  
  350.         //}
  351.         SetPort(savePort);    //    Get back to old port
  352.  
  353.         if ((dragResult != 0) && (dragResult != 0x80008000))
  354.             {
  355.             this->Nudge((short) (dragResult & 0xFFFF),(short) (dragResult >> 16));
  356.             }
  357.         }
  358.  
  359.     if (!commandKeyDown)
  360.         Select();
  361.     }
  362.  
  363. void
  364. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  365.     {
  366.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  367.     short        newHorizontalPosition,newVerticalPosition;
  368.     
  369.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  370.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  371.  
  372.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  373.     }
  374.  
  375. void
  376. TWindow::Grow(Point startPoint)
  377.     {
  378.     GrafPtr    oldPort;
  379.     long    newSize;
  380.     Rect    oldWindowRect,resizeLimits;
  381.     
  382.     GetPort(&oldPort);
  383.     
  384.     GetWindowSizeLimits(&resizeLimits);
  385.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  386.     if (newSize)
  387.         {
  388.         oldWindowRect = fWindow->portRect;
  389.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  390.         SetPort(fWindow);
  391.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  392.         }
  393.     
  394.     SetPort(oldPort);
  395.     }
  396.  
  397.  
  398. void
  399. TWindow::Zoom(short zoomState)
  400.     {
  401.     GrafPtr        oldPort;
  402.     FontInfo    systemFontInfo;
  403.     short        titleBarHeight;
  404.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  405.     short        amountOffscreen;
  406.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  407.     GDHandle    bestDevice;
  408.     
  409.     GetPort(&oldPort);
  410.  
  411.     //    Figure out the height of the title bar so we can properly position
  412.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  413.     //
  414.     //    This probably isn’t the best thing to do: A better way might be 
  415.     //    to diff the structure and content region rectangles?
  416.  
  417.     SetPort(gWindowManagerPort);
  418.     GetFontInfo(&systemFontInfo);
  419.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  420.     if ((titleBarHeight % 2) == 1)
  421.         titleBarHeight--;
  422.     if (titleBarHeight < kMinimumTitleBarHeight)
  423.         titleBarHeight = kMinimumTitleBarHeight;
  424.  
  425.  
  426.     //    Only do the voodoo magic if we are really “zooming” the window.
  427.  
  428.     if (zoomState == inZoomOut)
  429.         {
  430.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  431.         bestScreenRect.top += titleBarHeight;
  432.  
  433.         this->GetPerfectWindowSize(&perfectWindowRect);
  434.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  435.  
  436.         //    Take the zero-pined perfect window size and move it to
  437.         //    the top left of the    window’s content region.
  438.  
  439.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  440.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  441.  
  442.         
  443.         //    Does perfectWindowRect fit completely on the best screen?
  444.         
  445.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  446.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  447.             {
  448.             //    SectRect sez perfectWindowRect doesn’t completely fit
  449.             //    on the screen, so bump the window so that more of it fits.
  450.  
  451.             //    Make sure that the left edge of perfectWindowRect is forced
  452.             //    onto the best screen.  This is in case we are bumping
  453.             //    the window to the right.
  454.  
  455.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  456.             if (amountOffscreen > 0)
  457.                 {
  458.                 perfectWindowRect.left += amountOffscreen;
  459.                 perfectWindowRect.right += amountOffscreen;
  460.                 }
  461.  
  462.             //    Make sure that the left edge of perfectWindowRect is forced
  463.             //    onto the best screen.  This is in case we are bumping
  464.             //    the window downward to a new screen.
  465.     
  466.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  467.             if (amountOffscreen > 0)
  468.                 {
  469.                 perfectWindowRect.top += amountOffscreen;
  470.                 perfectWindowRect.bottom += amountOffscreen;
  471.                 }
  472.  
  473.             //    If right edge of window falls off the screen,
  474.             //        Move window to the left until the right edge IS on the screen
  475.             //        OR the left edge is at bestScreenRect.left
  476.  
  477.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  478.             if (amountOffscreen > 0)
  479.                 {
  480.                 //    Are we going to push the left edge offscreen? If so, change the
  481.                 //    offset so we move the window all the way over to the left.
  482.                 
  483.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  484.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  485.  
  486.                 perfectWindowRect.left -= amountOffscreen;
  487.                 perfectWindowRect.right -= amountOffscreen;
  488.                 }
  489.  
  490.             //    If bottom edge of window falls off the screen,
  491.             //        Move window to up until the bottom edge IS on the screen
  492.             //        OR the top edge is at bestScreenRect.top
  493.  
  494.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  495.             if (amountOffscreen > 0)
  496.                 {
  497.                 //    Are we going to push the top edge offscreen? If so, change the
  498.                 //    offset so we move the window just to the top.
  499.                 
  500.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  501.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  502.  
  503.                 perfectWindowRect.top -= amountOffscreen;
  504.                 perfectWindowRect.bottom -= amountOffscreen;
  505.                 }
  506.  
  507.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  508.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  509.                 {
  510.                 //    The edges of the window still fall offscreen,
  511.                 //    so make the window smaller until it fits.
  512.                 
  513.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  514.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  515.  
  516.                 //    If the right edge is still falling off,
  517.                 //        save space for Finder’s disk icons as well.
  518.  
  519.                 if (perfectWindowRect.right > bestScreenRect.right)
  520.                     {
  521.                     perfectWindowRect.right = bestScreenRect.right;
  522.                     
  523.                     //    If we were on the main screen, leave room for Finder icons, too.
  524.                     
  525.                     if (bestDevice == GetMainDevice())
  526.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  527.                     }
  528.                 }
  529.             }
  530.  
  531.         //    Stash our new rectangle inside of the Window’s dataHandle
  532.         //    so that ZoomWindow does the right thing.
  533.         
  534.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  535.         }
  536.  
  537.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  538.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  539.     
  540.     SetPort(fWindow);
  541.  
  542.     Rect    oldWindowRect = fWindow->portRect;
  543.     
  544.     ZoomWindow(fWindow,zoomState,false);
  545.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  546.  
  547.     SetPort(oldPort);
  548.     }
  549.  
  550. void
  551. TWindow::ShowHide(Boolean showFlag)
  552.     {
  553.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  554.     //    the global function, and not the method ShowHide. Unintended recursion
  555.     //    can do bad things to the unsuspecting programmer.
  556.     
  557.     //    Some C++ programmers would always prepend the “::” on function calls.
  558.     
  559.     ::ShowHide(fWindow,showFlag);
  560.     fIsVisible = showFlag;
  561.     }
  562.     
  563.  
  564. Boolean
  565. TWindow::EventFilter(EventRecord * /* theEvent */)
  566.     {
  567.     return false;
  568.     }
  569.     
  570.  
  571. void
  572. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  573.     {
  574.     *perfectSize = qd.screenBits.bounds;
  575.     }
  576.  
  577. void
  578. TWindow::GetWindowSizeLimits(Rect *limits)
  579.     {
  580.     limits->top = limits->left = kMinimumWindowSize;
  581.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  582.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  583.     }
  584.  
  585. void
  586. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  587.     {
  588.     }
  589.  
  590.  
  591. Boolean
  592. TWindow::IsVisible(void)
  593.     {
  594.     return fIsVisible;
  595.     }
  596.  
  597.  
  598. Boolean
  599. TWindow::CanClose(void)
  600.     {
  601.     return true;
  602.     }
  603.  
  604.  
  605. Boolean
  606. TWindow::Close(void)
  607.     {
  608.     WindowPtr    newFrontWindow = nil;
  609.     
  610.     if (FrontNonFloatingWindow() == fWindow)
  611.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  612.  
  613.     this->Activate(false);
  614.     DisposeWindow(fWindow);
  615.  
  616.     if (newFrontWindow)
  617.         HiliteAndActivateWindow(newFrontWindow,true);
  618.  
  619.     return true;
  620.     }
  621.  
  622.  
  623. Boolean
  624. TWindow::DeleteAfterClose(void)
  625.     {
  626.     return true;
  627.     }
  628.  
  629.  
  630. void
  631. TWindow::AdjustMenusBeforeMenuSelection(void)
  632.     {
  633.     }
  634.  
  635.     
  636. void
  637. TWindow::DoMenuSelection(short /* menu */, short /* item */)
  638.     {
  639.     }
  640.     
  641.  
  642. void
  643. TWindow::DoMenuCommand(unsigned long /* menuCommand */)
  644.     {
  645.     }
  646.  
  647.  
  648. OSErr
  649. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  650.     {
  651.     OSErr    result = dragNotAcceptedErr;
  652.     
  653.     switch (dragMessage)
  654.         {
  655.         case    kDragTrackingEnterWindow:
  656.             result = this->DragEnterWindow(theDrag);
  657.             break;
  658.         
  659.         case    kDragTrackingInWindow:
  660.             result = this->DragInWindow(theDrag);
  661.             break;
  662.             
  663.         case    kDragTrackingLeaveWindow:
  664.             result = this->DragLeaveWindow(theDrag);
  665.             break;
  666.             
  667.         default:
  668.             break;
  669.         }
  670.  
  671.     return result;
  672.     }
  673.  
  674.  
  675. OSErr
  676. TWindow::DragEnterWindow(DragReference /* theDrag */)
  677.     {
  678.     return dragNotAcceptedErr;
  679.     }
  680.  
  681.  
  682. OSErr
  683. TWindow::DragInWindow(DragReference /* theDrag */)
  684.     {
  685.     return dragNotAcceptedErr;
  686.     }
  687.  
  688.  
  689. OSErr
  690. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  691.     {
  692.     return dragNotAcceptedErr;
  693.     }
  694.     
  695.  
  696. OSErr
  697. TWindow::HandleDrop(DragReference /* theDrag */)
  698.     {
  699.     return dragNotAcceptedErr;
  700.     }
  701.  
  702.  
  703. ///////////////////////////////////////////////////////////////////////////
  704. //
  705. //    Utility Functions used for floating windows
  706. //
  707.  
  708. TWindow *
  709. GetWindowObject(WindowPtr aWindow)
  710.     {
  711.     short    wKind;
  712.     
  713.     if (aWindow != nil)
  714.         {
  715.         wKind = ((WindowPeek) aWindow)->windowKind;
  716.  
  717.         if (wKind >= userKind)
  718.             {
  719.             //    All windowKinds >= userKind are based upon TWindow
  720.  
  721.             return (TWindow *) GetWRefCon(aWindow);
  722.             }
  723.         }
  724.     return (TWindow *) nil;
  725.     }
  726.  
  727.  
  728. ////////////////////////////////////////////////////////////////////////
  729. //
  730. //    Utility functions
  731.  
  732.  
  733. pascal WindowPtr
  734. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  735.     {
  736.     if (gHasColorQuickdraw)
  737.         return GetNewCWindow(windowID,wStorage,behind);
  738.     else
  739.         return GetNewWindow(windowID,wStorage,behind);
  740.     }
  741.  
  742.  
  743. pascal WindowPtr
  744. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  745.     {
  746.     if (gHasColorQuickdraw)
  747.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  748.     else
  749.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  750.     }
  751.  
  752.  
  753. void
  754. DrawJustTheGrowIcon(WindowPtr aWindow)
  755.     {
  756.     GrafPtr        savedPort;
  757.     RgnHandle    savedClip = NewRgn();
  758.     Rect        growBoxRect;
  759.     
  760.     GetPort(&savedPort);
  761.     SetPort(aWindow);
  762.     GetClip(savedClip);
  763.  
  764.     //    clip to just the bottom right corner of the window
  765.     
  766.     growBoxRect.top = aWindow->portRect.bottom - kScrollbarWidth;
  767.     growBoxRect.bottom = aWindow->portRect.bottom;
  768.     growBoxRect.left = aWindow->portRect.right - kScrollbarWidth;
  769.     growBoxRect.right = aWindow->portRect.right;
  770.     ClipRect(&growBoxRect);
  771.  
  772.     DrawGrowIcon(aWindow);
  773.  
  774.     SetClip(savedClip);
  775.     DisposeRgn(savedClip);    
  776.  
  777.     SetPort(savedPort);
  778.     }
  779.     
  780.  
  781. WindowPtr
  782. LastFloatingWindow(void)
  783.     {
  784.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  785.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  786.     
  787.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  788.         {
  789.         if (aWindow->visible)
  790.             lastFloater = (WindowPtr) aWindow;
  791.  
  792.         aWindow = (WindowPeek) aWindow->nextWindow;
  793.         }
  794.     return(lastFloater);
  795.     }
  796.  
  797. // Part of 3.3 universal interfaces
  798. /*    
  799. WindowPtr
  800. FrontNonFloatingWindow(void)
  801.     {
  802.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  803.  
  804.     //    Skip over floating windows
  805.         
  806.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  807.         aWindow = (WindowPeek) aWindow->nextWindow;
  808.  
  809.     //    Skip over invisible, but otherwise normal windows
  810.     
  811.     while (aWindow && (aWindow->visible == 0))
  812.         aWindow = (WindowPeek) aWindow->nextWindow;
  813.         
  814.     return (WindowPtr) aWindow;
  815.     }
  816. */
  817.  
  818. void
  819. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  820.     {
  821.     GrafPtr        oldPort;
  822.     TWindow    *    wobj = GetWindowObject(aWindow);
  823.     
  824.     if (aWindow)
  825.         {
  826.         HiliteWindow(aWindow,active);
  827.  
  828.         if (wobj != nil)
  829.             {
  830.             GetPort(&oldPort);
  831.             SetPort(aWindow);
  832.             wobj->Activate(active);
  833.             SetPort(oldPort);
  834.             }    
  835.         }
  836.     }
  837.  
  838. void
  839. SuspendResumeWindows(Boolean resuming)
  840.     {
  841.     //    When we suspend/resume, hide/show all the visible floaters
  842.     
  843.     HiliteShowHideFloatingWindows(resuming,true);
  844.     }
  845.  
  846. void
  847. HiliteWindowsForModalDialog(Boolean hiliting)
  848.     {
  849.     //    When we display a modal dialog, we need to unhighlight
  850.     //    all visible floaters. We also need to re-hilite them
  851.     //    afterwards.
  852.     
  853.     HiliteShowHideFloatingWindows(hiliting,false);
  854.     }
  855.  
  856. void
  857. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  858.     {
  859.     WindowPeek    aWindow;
  860.     TWindow *    wobj;
  861.     
  862.     HiliteAndActivateWindow(FrontNonFloatingWindow(),hiliting);
  863.  
  864.     aWindow = (WindowPeek) LMGetWindowList();
  865.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  866.         {
  867.         wobj = GetWindowObject((WindowPtr) aWindow);
  868.         
  869.         //    If we are hiding or showing, only hide/show windows
  870.         //    that were visible to begin with.
  871.         
  872.         //    NOTE:    We use our copy of the visible flag so we can
  873.         //            automatically show floaters on a resume event.
  874.         
  875.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  876.         //            really need the “::” on ShowHide, but as long
  877.         //            as we’re trying to avoid ambiguity.
  878.         
  879.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  880.             ::ShowHide((WindowPtr) aWindow,hiliting);
  881.             
  882.         //    All floaters are hilited if any floater is hilited
  883.  
  884.         HiliteWindow((WindowPtr) aWindow,hiliting);
  885.         aWindow = (WindowPeek) aWindow->nextWindow;
  886.         }
  887.     }
  888.  
  889.  
  890. ///////////////////////////////////////////////////////////////////////////
  891. //
  892. //    Routines used for dealing with windows and multiple screens
  893. //
  894.  
  895. pascal void
  896. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  897.     {
  898.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  899.     long                                windowAreaOnThisScreen;
  900.     Rect                                windowRectOnThisScreen;
  901.     
  902.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  903.  
  904.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  905.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  906.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  907.  
  908.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  909.         {
  910.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  911.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  912.         }
  913.     }
  914.  
  915.  
  916. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  917.  
  918.  
  919. void
  920. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  921.     {
  922.     RgnHandle                            copyOfWindowStrucRgn;
  923.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  924.  
  925.     //    Use DeviceLoop to find out what GDevice contains the largest
  926.     //    portion of the supplied window.
  927.     //
  928.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  929.     //            the window strucRgn, not contRgn.
  930.  
  931.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  932.     deviceLoopData.fLargestArea = -1;
  933.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  934.     
  935.     copyOfWindowStrucRgn = NewRgn();
  936.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  937.  
  938.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  939.  
  940.     DisposeRgn(copyOfWindowStrucRgn);
  941.     
  942.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  943.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  944.  
  945.     //    Leave some space around the edges of the screen so window look good, AND
  946.     //    if the best device is the main screen, leave space for the Menubar
  947.     
  948.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  949.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  950.         theBestScreenRect->top += GetMBarHeight();
  951.     }
  952.  
  953.  
  954. ///////////////////////////////////////////////////////////////////////////
  955. //
  956. //    Drag Manager callback routines which dispatch to a window’s method
  957. //
  958.  
  959. pascal OSErr
  960. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  961.     {
  962.     TWindow *wobj = GetWindowObject(theWindow);
  963.     
  964.     if (wobj)
  965.         return(wobj->HandleDrag(dragMessage,theDrag));
  966.     else
  967.         return dragNotAcceptedErr;
  968.     }
  969.  
  970.     
  971. pascal OSErr
  972. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  973.     {
  974.     TWindow *wobj = GetWindowObject(theWindow);
  975.     
  976.     if (wobj)
  977.         return(wobj->HandleDrop(theDrag));
  978.     else
  979.         return dragNotAcceptedErr;
  980.     }
  981.